package cn.getech.data.development.utils;

import cn.getech.data.development.dto.JobNodeConfigDto;
import cn.getech.data.development.entity.JobNodeConfig;
import cn.getech.data.development.entity.SapConnectField;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/***
 * 处理特殊的jobNodeConfig转换不了的对象
 */
public class JobNodeConfigUtil {

    public static JobNodeConfig changeJobOutPutTableIds(JobNodeConfigDto jobNodeConfigDto) {
        JobNodeConfig jobNodeConfig = new JobNodeConfig();
        List<String> changeJobOutPutTableIds = new ArrayList<>();
        if (null != jobNodeConfigDto && null != jobNodeConfigDto.getJobOutPutTableIds() && jobNodeConfigDto.getJobOutPutTableIds().size() > 0) {
            List<String> jobOutPutTableIds = jobNodeConfigDto.getJobOutPutTableIds();
            for (String jobOutPutTableId : jobOutPutTableIds) {
                if(StringUtils.isNotEmpty(jobOutPutTableId) && !Objects.equals("[]",jobOutPutTableId.trim())){
                    String sapNameSub = jobOutPutTableId.substring(1, jobOutPutTableId.length() - 1);
                    String[] sapArray = sapNameSub.split(",");
                    List<String> sapList = Arrays.asList(sapArray);
                    for (int i = 0; i < sapList.size(); i++) {
                        String s = sapList.get(i);
                        if(StringUtils.isNotEmpty(s)){
                            s = s.replace("\"","");
                            changeJobOutPutTableIds.add(s);
                        }
                    }
                }
            }
        }
        jobNodeConfig.setJobOutPutTableIds(changeJobOutPutTableIds);
        return jobNodeConfig;
    }


    public static JobNodeConfig changeSapOutPutTableName(JobNodeConfigDto jobNodeConfigDto) {
        JobNodeConfig jobNodeConfig = new JobNodeConfig();
        List<String> sapOutputTableNames = new ArrayList<>();
        if (null != jobNodeConfigDto && null != jobNodeConfigDto.getSap_output_table_name() && jobNodeConfigDto.getSap_output_table_name().size() > 0) {
            List<String> sap_output_table_name_older = jobNodeConfigDto.getSap_output_table_name();
            for (String sapName : sap_output_table_name_older) {

                if(StringUtils.isNotEmpty(sapName) && !Objects.equals("[]",sapName.trim())){
                    String sapNameSub = sapName.substring(1, sapName.length() - 1);
                    String[] sapArray = sapNameSub.split(",");
                    List<String> sapList = Arrays.asList(sapArray);
                    for (String s : sapList) {
                        sapOutputTableNames.add(s);
                    }
                }
            }
        }
        jobNodeConfig.setSap_output_table_name(sapOutputTableNames);
        return jobNodeConfig;
    }

    public static SapConnectField changeSapConnectFields(JobNodeConfigDto jobNodeConfigDto) {
        SapConnectField sapConnectField = null;
        //获取SAP数据源配置（和最开始的数据源不同，是单独保存的配置信息）
        JSONObject jsonObject = new JSONObject();
        String sapConnectFieldInfos = jobNodeConfigDto.getSapConnectFieldInfos();
        if (StringUtils.isNotEmpty(sapConnectFieldInfos) && !Objects.equals("null",sapConnectFieldInfos.toLowerCase())) {
            String[] sapParams = {"fieldName", "fieldKind", "fieldValue", "fieldAlias", "fieldInputType", "fieldDataType"};
            List<String> list = new ArrayList<>();
            for (String sapParam : sapParams
            ) {
                list.add(sapParam);
            }
            jsonObject = JSONObject.parseObject(sapConnectFieldInfos);
            if (jsonObject != null && jsonObject.containsKey("ainputParams")) {
                String ainputParams = jsonObject.get("ainputParams").toString();
                JSONArray jsonArray = JSONArray.parseArray(ainputParams);
                JSONArray jsonArray1 = new JSONArray();
                for (Object obj : jsonArray
                ) {
                    String string = JSONObject.toJSONString(obj);
                    JSONObject jsonObject1 = JSONObject.parseObject(string);
                    List<String> keys = new ArrayList<>(jsonObject1.keySet());
                    list.removeAll(keys);
                    for (String s : list
                    ) {
                        jsonObject1.put(s, "");
                    }
                    jsonArray1.add(jsonObject1);
                }
                jsonObject.put("ainputParams", jsonArray1);
            }
        }
        if (jsonObject.size() != 0) {
            sapConnectField = mapToSapConnectField(jsonObject);
            return sapConnectField;
        }
        return null;
    }

    public static SapConnectField mapToSapConnectField(Map<String, Object> map) {
        if (map == null)
            return null;
        SapConnectField obj;
        try {
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(map));
            obj = JSON.parseObject(jsonObject.toJSONString(), new TypeReference<SapConnectField>() {
            });
            return obj;
        } catch (Exception e) {
            return null;
        }
    }

    public static List<Map<String, String>> changePartition(JobNodeConfigDto jobNodeConfigDto) {
        if (null != jobNodeConfigDto && StringUtils.isNotEmpty(jobNodeConfigDto.getOutput_data_partition())) {
            String output_data_partition_older = jobNodeConfigDto.getOutput_data_partition();
            List<Map<String, String>> output_data_partition = new ArrayList<>();
            if (output_data_partition_older != null && !"".equals(output_data_partition_older)) {
                String[] output_data_partitions = output_data_partition_older.split(",");
                for (String s : output_data_partitions
                ) {
                    if (!s.contains("=")) {
                        continue;
                    }
                    Map<String, String> presentMap = new HashMap<>();
                    String fieldName = s.substring(0, s.indexOf("="));
                    String fieldValue = s.substring(s.indexOf("=") + 1);
                    presentMap.put("fieldName", fieldName);
                    presentMap.put("fieldValue", fieldValue);
                    output_data_partition.add(presentMap);
                }
            }
            return output_data_partition;
        }
        return null;
    }

}
